﻿/*
 * ========================================================================
 * Copyright(c) 2008-2010北京高阳金信信息技术有限公司, All Rights Reserved.
 * ========================================================================
 *  邮件基类
 * 
 * 作者：周燕龙     时间：2010-06-18至21
 * 版本：V1.0
 * 
 * 修改者：         时间：                
 * 修改说明：
 * ========================================================================
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;

namespace BaseMailUtil
{
    public abstract class Email
    {
        public abstract bool SendMail(MailInfo curMsg);

        public abstract bool RecvMail(string I_MailServer, string I_UserName, string I_UserPwd, int I_Port);

        public abstract Dictionary<string, string> GetMail(string I_MailServer, string I_UserName, string I_UserPwd, int I_Port);

        public abstract void DelMail(string I_MailServer, string I_UserName, string I_UserPwd, int I_Port);

        /// <summary>
        /// 字符转对象
        /// </summary>
        /// <param name="json">字符</param>
        /// <param name="returnType">类型</param>
        /// <returns></returns>
        public  object GetObjectFromJson(string json, Type returnType)
        {
            using (System.IO.Stream stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                var desSerializer = new DataContractJsonSerializer(returnType);
                var result = desSerializer.ReadObject(stream);
                return result;
            }
        }

        /// <summary>
        /// 对象转换为json
        /// </summary>
        /// <typeparam name="TPara">对象类型</typeparam>
        /// <param name="data">代转数据</param>
        /// <returns></returns>
        public string GetJSONString<TPara>(TPara data) where TPara : class
        {
            var streamReader = new System.IO.StreamReader(new System.IO.MemoryStream());
            var serializer = new DataContractJsonSerializer(typeof(TPara));
            serializer.WriteObject(streamReader.BaseStream, data);
            streamReader.BaseStream.Position = 0;
            string result = streamReader.ReadToEnd();
            streamReader.Close();
            return result;

        }

        /// <summary>
        /// json转换为对象
        /// </summary>
        /// <typeparam name="TResult">对象类型</typeparam>
        /// <param name="json">json字符</param>
        /// <returns></returns>
        public TResult GetObjectFromJsonString<TResult>(string json)
        {
            using (System.IO.Stream stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                var desSerializer = new DataContractJsonSerializer(typeof(TResult));
                var result = (TResult)desSerializer.ReadObject(stream);
                return result;
            }
        }


    }

    /// <summary>
    /// 邮件信息类
    /// </summary>
    public class MailInfo
    {
        private string _mailServer = string.Empty; 
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string MailServer
        {
            get { return _mailServer; }
            set { _mailServer = value; }
        }

        private string _serverUsername = string.Empty; 
        /// <summary>
        /// 服务器登录用户名
        /// </summary>
        public string ServerUsername
        {
            get { return _serverUsername; }
            set { _serverUsername = value; }
        }


        private string _serverPassword = string.Empty;  
        /// <summary>
        /// 服务器登录密码
        /// </summary>
        public string ServerPassword
        {
            get { return _serverPassword; }
            set { _serverPassword = value; }
        }


        private string _mailFrom = string.Empty;  
        /// <summary>
        /// 发送方地址
        /// </summary>
        public string MailFrom
        {
            get { return _mailFrom; }
            set { _mailFrom = value; }
        }


        private string _mailTo = string.Empty; 
        /// <summary>
        /// 接收方地址
        /// </summary>
        public string MailTo
        {
            get { return _mailTo; }
            set { _mailTo = value; }
        }


        private string _mailSubject = string.Empty;  
        /// <summary>
        /// 邮件标题
        /// </summary>
        public string MailSubject
        {
            get { return _mailSubject; }
            set { _mailSubject = value; }
        }


        private string _mailBody = string.Empty; 
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string MailBody
        {
            get { return _mailBody; }
            set { _mailBody = value; }
        }


        private string _mailCharset = "gb2312"; 
        /// <summary>
        /// 采用的字符集
        /// </summary>
        public string MailCharset
        {
            get { return _mailCharset; }
            set { _mailCharset = value; }
        }


        private byte _iPriority = 3;
        /// <summary>
        /// 优先级
        /// </summary>
        public byte IPriority
        {
            get { return _iPriority; }
            set { _iPriority = value; }
        }

    }
}
